<template>
  <div class="entWrap">
    <!-- 全局属性 -->
    <AttrCfg
      @filedChange="filedChange"
      :form="$store.state.formJson.formObj[`${type}`][`${typeSecond}`].attrForm"
      :filedList="$store.state.formJson.formObj[`${type}`][`${typeSecond}`].filedList"
      :type="type"
    ></AttrCfg>
    <!-- 字段配置 -->
    <FiledCfg
      :type="type"
      :entFiledsList="entFiledsList"
      :dataSource="$store.state.formJson.formObj[`${type}`][`${typeSecond}`].filedList"
    ></FiledCfg>
  </div>
</template>

<script>
import { dropKey } from '@/views/businessAssembly/jsonComponent/constant/dropKey.js'
export default {
  props: {
    type: {
      type: String,
      default: ''
    }, // 一级分类
    typeSecond: {
      type: String,
      default: ''
    } // 二级分类
  },
  data() {
    return {
      entFiledsList: [], // 企业表字段列表
      tempObj: {} // 选中表对象
    }
  },
  components: {
    AttrCfg: () => import('@/views/businessAssembly/jsonComponent/base/attrCfg.vue'),
    FiledCfg: () => import('@/views/businessAssembly/jsonComponent/base/filedCfg.vue')
  },
  mounted() {
    if (this.type == 'DROP') {
      if (!this.$store.state.formJson.formObj[`${this.type}`][`${this.typeSecond}`].filedList.length) {
        let arry = []
        for (let key in dropKey[this.typeSecond]) {
          let tempObj = {}
          tempObj = { b: key, mapping: '', describe: dropKey[this.typeSecond][key] }
          if (key == 'adid') {
            tempObj.isparam = true
          } else if (key == 'preacceptsectionid' || key == 'e_lng' || key == 'e_lat') {
            tempObj.isparam = false
          }
          arry.push(tempObj)
        }
        this.$store.state.formJson.formObj[`${this.type}`][`${this.typeSecond}`].filedList = arry
      }
    }
    if (this.$store.state.formJson.listData?.length) {
      this.$store.state.formJson.listData.forEach(v => {
        this.tempObj[v.title] = v.title
      })
    }
  },
  methods: {
    // 字段改变
    filedChange(list, type, tableName) {
      if (type) {
        let listCopy = list.map(item => {
          return {
            t: item.fieldname,
            b: '',
            mapping: '',
            describe: item.comments,
            hidden: 'false',
            sort: null,
            rules: []
          }
        })
        if (listCopy?.length) {
          listCopy.forEach(v => {
            let temp = tableName + '#' + v.t
            if (temp in this.tempObj) {
              v.mapping = temp
            }
          })
        }
        this.$store.state.formJson.formObj[`${this.type}`][`${this.typeSecond}`].filedList = listCopy
      } else {
        let arry = []
        if (list?.length) {
          arry = list.map(item => {
            return {
              title: `${item.fieldname} - ${item.comments}`,
              value: item.fieldname
            }
          })
        }
        this.entFiledsList = arry
      }
    }
  }
}
</script>

<style lang="less" scoped>
.entWrap {
  height: 600px;
  overflow-y: auto;
}
</style>
